﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using XYTXL.Data;
using XYTXL.API;

namespace XYTXL.Web.View
{
    public partial class CWReturn : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string cert = "601CCD54D7EEF7A3E050230A0164565F601CCD54D7EFF7A3E050230A0164565F601CCD54D7F0F7A3E050230A0164565F601CCD54D7F1F7A3E050230A0164565F601CCD54D7F2F7A3E050230A0164565F601CCD54D7F3F7A3E050230A0164565F601CCD54D7F4F7A3E050230A0164565F601CCD54D7F5F7A3E050230A0164565F601CCD54D7F6F7A3E050230A0164565F601CCD54D7F7F7A3E050230A0164565F601CCD54D7F8F7A3E050230A0164565F601CCD54D7F9F7A3E050230A0164565F601CCD54D7FAF7A3E050230A0164565F601CCD54D7FBF7A3E050230A0164565F601CCD54D7FCF7A3E050230A0164565F601CCD54D7FDF7A3E050230A0164565F601CCD54D7FEF7A3E050230A0164565F601CCD54D7FFF7A3E050230A0164565F601CCD54D800F7A3E050230A0164565F601CCD54D801F7A3E050230A0164565F601CCD54D802F7A3E050230A0164565F601CCD54D803F7A3E050230A0164565F601CCD54D804F7A3E050230A0164565F601CCD54D805F7A3E050230A0164565F601CCD54D806F7A3E050230A0164565F601CCD54D807F7A3E050230A0164565F601CCD54D808F7A3E050230A0164565F601CCD54D809F7A3E050230A0164565F601CCD54D80AF7A3E050230A0164565F601CCD54D80BF7A3E050230A0164565F601CCD54D80CF7A3E050230A0164565F601CCD54D80DF7A3E050230A0164565F";
            string sysid = "08";
            string subsysid = "201703";
            string data = Context.Request["data"].ToString();
            XYTXL.API.CommonHelp.WriteLOG("1-开始接收返回数据：" + data);
            //CommonHelp.WriteLOG("开始缴费" + ksny + "," + dm + "," + jwcode);
            string sign = Context.Request["sign"].ToString();
            XYTXL.API.CommonHelp.WriteLOG("2-开始接收返回数据签名：" + sign);
            string st = sysid + subsysid + cert + data;
            string sign1 = GetMd5Hash(st);

            XYTXL.API.CommonHelp.WriteLOG("3-校验签名结果：" + sign1);

            if (sign == sign1)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(data);
                XmlNodeList xn0 = xmlDoc.SelectSingleNode("payResult").ChildNodes;

                //查找二级节点的内容或属性
                int intDDH = 0;
                decimal jfje = 0;
                string trade_no = "";
                
                XYTXL.API.CommonHelp.WriteLOG("4-获取节点数据：" + Newtonsoft.Json.JsonConvert.SerializeObject(xn0));


                foreach (XmlNode node in xn0)
                {
                    if (node.Name == "billno")
                    {
                        intDDH = int.Parse(node.InnerText.Trim()); //匹配二级节点的内容
                    }
                    if (node.Name == "billamt")
                    {
                        jfje = decimal.Parse(node.InnerText.Trim()); //匹配二级节点的内容
                    }
                    if (node.Name == "trade_no")
                    {
                        trade_no = node.InnerText.Trim().ToString (); //匹配二级节点的内容
                    }
                    
                }
                //验签成功，业务逻辑处理程序
                xy_jzmx model = new xy_jzmxB().GetEntities(d => d.ddh == intDDH).FirstOrDefault();
                if (model != null)
                {
                    model.iszf = "Y";
                    model.jfje = jfje;
                    model.trade_no = trade_no;
                    model.zfrq = DateTime.Now;
                    new xy_jzmxB().Update(model);
                }
                msg.Value = "支付成功";
                msgtype.Value = "1";
                //解析xml，跟新xy_jzmx表，jfje,zfrq，iszf字段
                //在返回页面上显示支付成功图案。
            }
            else
            {
                //验签失败，显示验签失败或支付不成功。
                msg.Value = "验签失败或支付不成功";
                msgtype.Value = "3";

            }


            //Md5(sysid + subsysid + cert + data)
        }

        public static string GetMd5Hash(string input)
        {
            //MD5 md5Hash = new MD5();
            MD5 md5Hash = MD5.Create();
            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
    }
}